$(document).ready(() => {
    $("#input-id").rating();
    $("#input-id").rating({ 'size': 'xs' });

    const serverHost = window.config.serverHost;

    let url = location.search;
    let result = url.split("=");
    let orderId = result[1];

    $('#foodsPic').text('');
    $.ajax({
        url: `${serverHost}/order/detail?orderId=${orderId}`,
        type: 'GET',
        success: data => {
            console.log(data)

            let status = data.orderStatus
            let statusContent = ''
            switch (status) {
                case 0:
                    statusContent = '等待商家接单'
                    break;
                case 1:
                    statusContent = '商家已拒单'
                    break;
                case 2:
                    statusContent = '商家已接单'
                    break;
                case 3:
                    statusContent = '配送中'
                    break;
                case 4:
                    statusContent = '订单已完成'
                    document.getElementById("complaint_btn").style.display = "";
                    document.getElementById("comment_div").style.display = "";
                    break;
                case 5:
                    statusContent = '订单已取消'
                    break;
            }
            $('#status').text(statusContent)

            let orderId = data.orderId
            $('#orderId').text(orderId)  // Set Order id

            let merchantName = data.merchantName
            $('#merchantName').text(merchantName)

            let merchantId = data.merchantId
            $('#merchantId').val(merchantId)

            let confirmTime = data.confirmTime
            $('#confirmTime').text(confirmTime)

            let addressContactPerson = data.addressContactPerson
            $('#addressContactPerson').text(addressContactPerson)

            let addressContactTel = data.addressContactTel
            $('#addressContactTel').text(addressContactTel)

            let addressName = data.addressName
            $('#addressName').text(addressName)

            let foodsList = data.orderFoods
            let pics = ''
            let foodsContent = ''
            let countsPrice = 0.00
            foodsList.forEach(item => {
                pics += `<a href="#"><img src="${item.foodPic}" style="width: 31%; float: left; padding: 5px;"></a>`
                foodsContent += `<p><span>菜品：</span> <span>${item.foodName}</span></p>
                                <p><span>数量：</span> <span>${item.count}</span></p>`
                countsPrice += item.foodPrice
            });
            $('#foodsPic').html(pics)
            $('#foodsContent').html(foodsContent)
            $('#countsPrice').text(countsPrice.toFixed(2))
        },
        error: () => {
            console.log('Request Order Detail Fail.')
        }
    })
})

$('#complaint_submit').on('click', () => {
    let complaintText = $('#complaint_text').val()
    console.log(complaintText)
    let customerId = JSON.parse(window.localStorage.getItem('user')).customerId
    let merchantId = $('#merchantId').val()

    console.log(complaintText, customerId, merchantId)
    const serverHost = window.config.serverHost;
    $.ajax({
        // url: `${serverHost}/order/complaint`,
        url: 'http://10.222.29.195:8080/amc-a-v3/admin/complaint/doComplain',
        type: 'POST',
        data: {
            merchantId: merchantId,
            customerId: customerId,
            complaintText: complaintText
        },
        success: data => {
            console.log(data)
            if (data == 'success') {
                $('#complaint').modal('hide')
                $('#complaintSuccess').modal('show')
            } else {
                $('#complaint_errorText').text('Complaint Fail.Please Try Again Latter.')
            }
        },
        error: () => {
            $('#complaint_errorText').text('Server Error.Please Try Again Latter.')
        }
    })
})

$('#comment-btn').on('click', () => {
    let commentText = $('#comment_text').val();

    let grade = parseInt(($('.rating-stars .filled-stars').css('width')).split('px')[0]);
    let first = parseInt(grade / 35)
    let commentScore = (grade % 35).toFixed(2) > 17.50 ? (first + 1).toFixed(2) : (first + 0.5).toFixed(2)

    let orderId = $('#orderId').text()
    let merchantId = $('#merchantId').val()
    let customerId = JSON.parse(window.localStorage.getItem('user')).customerId
    console.log(orderId, customerId, merchantId, commentText, commentScore)

    const serverHost = window.config.serverHost
    $.ajax({
        url: `${serverHost}/comment/publish`,
        type: 'POST',
        data: {
            merchantId: merchantId,
            customerId: customerId,
            orderId: orderId,
            commentText: commentText,
            commentScore: commentScore
        },
        success: data => {
            console.log(data)
            let status = data.status
            if (status == 'SUCCESS') {
                $('#comment_text').val('')
                $('#commentResult').text('Comment Success!')
                $('#commentSuccess').modal('show')
                // let ws = new WebSocket("ws://10.222.29.145:8880/acmsystem-m-v2.0/commentnew");
                // ws.onopen = function (evt) {
                //     ws.send("COMMENT_SUCCESS");
                // }
            } else {
                $('#commentSuccess').modal('show')
                $('#commentResult').text('Comment Fail. Please Try Again Latter.')
            }
        },
        error: () => {
            $('#commentResult').text('Server Error. Please Try Again Latter.')
        }
    })
})